home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / PowerPlant / Property 2.1 / property.cp next >
Encoding:
Text File  |  1996-06-30  |  5.5 KB  |  241 lines  |  [TEXT/CWIE]

  1. /*
  2.     property is © 1996 Boxes Objects Links Design Pty Ltd.  All Rights Reserved. 
  3.     You use this software at your own risk, etc. Permission is given to Timothy C.
  4.     Delaney to use property. Permission is given for all others to use property if
  5.     acknowledgement of this copyright is given in publically-released software.
  6.     Acknowledgement should consist of a statement equivalent to "Sections of this
  7.     program are © Boxes Objects Links Design Pty Ltd", visible in an "About..." box
  8.     or splash screen.
  9.                                                                                         */
  10.  
  11. #include "property.h"
  12.  
  13. template <EPropertyAccess access, class T>
  14. property<access, T>::property (void * const        theObject,
  15.                                T                &field        )
  16. :    mObject(theObject)
  17. {
  18.     mGetAccess = internalAccess_None;
  19.     mSetAccess = internalAccess_None;
  20.  
  21.     mGet.mGetField = NULL;
  22.     mSet.mSetField = NULL;
  23.  
  24.     if (access == propertyAccess_ReadOnly)
  25.     {
  26.         mGetAccess = internalAccess_Field;
  27.         mGet.mGetField = &field;
  28.     }
  29.     else if (access == propertyAccess_WriteOnly)
  30.     {
  31.         mSetAccess = internalAccess_Field;
  32.         mSet.mSetField = &field;
  33.     }
  34.  
  35.     MatchAccess_();
  36. }
  37.  
  38. template <EPropertyAccess access, class T>
  39. property<access, T>::property (void * const        theObject,
  40.                                void                (*getFunc) (void * const, T &    )    )
  41. :    mObject(theObject),
  42.     mGetAccess(internalAccess_Func), mSetAccess(internalAccess_None)
  43. {
  44.     MatchAccess_();
  45.  
  46.     mGet.mGetFunc = getFunc;
  47.     mSet.mSetField = NULL;
  48. }
  49.  
  50. template <EPropertyAccess access, class T>
  51. property<access, T>::property (void * const        theObject,
  52.                                void                (*setFunc) (void * const, const T &    )    )
  53. :    mObject(theObject),
  54.     mGetAccess(internalAccess_None), mSetAccess(internalAccess_Func)
  55. {
  56.     MatchAccess_();
  57.  
  58.     mGet.mGetField = NULL;
  59.     mSet.mSetFunc = setFunc;
  60. }
  61.  
  62. template <EPropertyAccess access, class T>
  63. property<access, T>::property (void * const theObject,
  64.                                T            &getField,
  65.                                T            &setField    )
  66. :    mObject(theObject),
  67.     mGetAccess(internalAccess_Field), mSetAccess(internalAccess_Field)
  68. {
  69.     MatchAccess_();
  70.  
  71.     mGet.mGetField = &getField;
  72.     mSet.mSetField = &setField;
  73. }
  74.  
  75. template <EPropertyAccess access, class T>
  76. property<access, T>::property (void * const        theObject,
  77.                                void                (*getFunc) (void * const, T &    ),
  78.                                T                &setField                    )
  79. :    mObject(theObject),
  80.     mGetAccess(internalAccess_Func), mSetAccess(internalAccess_Field)
  81. {
  82.     MatchAccess_();
  83.  
  84.     mGet.mGetFunc = getFunc;
  85.     mSet.mSetField = &setField;
  86. }
  87.  
  88. template <EPropertyAccess access, class T>
  89. property<access, T>::property (void * const        theObject,
  90.                                T                &getField,
  91.                                void                (*setFunc) (void * const, const T&)        )
  92. :    mObject(theObject),
  93.     mGetAccess(internalAccess_Field), mSetAccess(internalAccess_Func)
  94. {
  95.     MatchAccess_();
  96.  
  97.     mGet.mGetField = &getField;
  98.     mSet.mSetFunc = setFunc;
  99. }
  100.  
  101. template <EPropertyAccess access, class T>
  102. property<access, T>::property (void * const        theObject,
  103.                                void                (*getFunc) (void * const, T&),
  104.                                void                (*setFunc) (void * const, const T&)        )
  105. :    mObject(theObject),
  106.     mGetAccess(internalAccess_Func), mSetAccess(internalAccess_Func)
  107. {
  108.     MatchAccess_();
  109.  
  110.     mGet.mGetFunc = getFunc;
  111.     mSet.mSetFunc = setFunc;
  112. }
  113.  
  114. template <EPropertyAccess access, class T>
  115. property<access, T>::~property (void    )
  116. {
  117. }
  118.  
  119. template <EPropertyAccess access, class T>
  120. inline bool    property<access, T>::Readable (void    )
  121. {
  122.     return mGetAccess != internalAccess_None;
  123. }
  124.  
  125. template <EPropertyAccess access, class T>
  126. inline bool    property<access, T>::Writeable (void    )
  127. {
  128.     return mSetAccess != internalAccess_None;
  129. }
  130.  
  131. template <EPropertyAccess access, class T>
  132. inline bool property<access, T>::ReadFunc (void    )
  133. {
  134.     return mGetAccess == internalAccess_Func;
  135. }
  136.  
  137. template <EPropertyAccess access, class T>
  138. inline bool property<access, T>::WriteFunc (void    )
  139. {
  140.     return mSetAccess == internalAccess_Func;
  141. }
  142.  
  143. template <EPropertyAccess access, class T>
  144. inline void    property<access, T>::ThrowAccess_ (void    )    throw (EPropertyAccess)
  145. {
  146.     throw access;
  147. }
  148.  
  149. template <EPropertyAccess access, class T>
  150. inline void    property<access, T>::MatchAccess_ (void    )    throw (EPropertyAccess)
  151. {
  152.     if (access == propertyAccess_ReadWrite && (!Readable() || !Writeable()))
  153.     {
  154.         ThrowAccess_();
  155.     }
  156.     else if (access == propertyAccess_ReadOnly && Writeable())
  157.     {
  158.         ThrowAccess_();
  159.     }
  160.     else if (access == propertyAccess_WriteOnly && Readable())
  161.     {
  162.         ThrowAccess_();
  163.     }
  164. }
  165.  
  166. template <EPropertyAccess access, class T>
  167. property<access, T>::operator T (void    )
  168. {
  169.     if (Readable())
  170.     {
  171.         if (ReadFunc())
  172.         {
  173.             T    tempField;
  174.  
  175.             mGet.mGetFunc(mObject, tempField);
  176.             return tempField;
  177.         }
  178.         else
  179.         {
  180.             return *mGet.mGetField;
  181.         }
  182.     }
  183.     else
  184.     {
  185.         ThrowAccess_();
  186.         return *mGet.mGetField;        //    shut up warning about missing return value
  187.     }
  188. }
  189.  
  190. template <EPropertyAccess access, class T>
  191. property &    property<access, T>::operator= (const T    &newValue    )
  192. {
  193.     if (Writeable())
  194.     {
  195.         if (WriteFunc())
  196.         {
  197.             mSet.mSetFunc(mObject, newValue);
  198.         }
  199.         else
  200.         {
  201.             *mSet.mSetField = newValue;
  202.         }
  203.     }
  204.     else
  205.     {
  206.         ThrowAccess_();
  207.     }
  208.  
  209.     return *this;
  210. }
  211.  
  212. template <EPropertyAccess access, class T>
  213. property &    property<access, T>::operator= (property<access, T>    newProperty    )
  214. {
  215.     return (*this = (T) newProperty);
  216. }
  217.  
  218. template <EPropertyAccess access, class T>
  219. property &    property<access, T>::operator+= (const T    &newValue    )
  220. {
  221.     return (*this = *this + newValue);
  222. }
  223.  
  224. template <EPropertyAccess access, class T>
  225. property &    property<access, T>::operator-= (const T    &newValue    )
  226. {
  227.     return (*this = *this - newValue);
  228. }
  229.  
  230. template <EPropertyAccess access, class T>
  231. property &    property<access, T>::operator*= (const T    &newValue    )
  232. {
  233.     return (*this = *this * newValue);
  234. }
  235.  
  236. template <EPropertyAccess access, class T>
  237. property &    property<access, T>::operator/= (const T    &newValue    )
  238. {
  239.     return (*this = *this / newValue);
  240. }
  241.